﻿/*======================================*\
* Author: Vincent
* LastModifyUser: Vincent
* Description:  
* CreateTime： 2012-05
* Remark:  
\*======================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using AppCode.Util;

namespace AppCode.Mapping
{
    public class ParamClassHandler
    {
        /// <summary>
        /// 命名空间
        /// </summary>
        protected const string CODECREATE_NAMESPACENAME = "$ProjectNameSpace$";

        /// <summary>
        /// 公有属性替换
        /// </summary>
        protected const string CODECREATE_PUBLICEPROPERTY = "$PublicPropertys$";

        private StringBuilder _sbMain = null;

        /// <summary>
        /// 获取表头信息[字段]
        /// </summary>
        /// <param name="connStr"></param>
        /// <param name="cmdStr"></param>
        /// <returns></returns>
        public DataSet GetTableInfo(string connStr,string cmdStr)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                var ds = new DataSet();
                SqlDataAdapter sdp = new SqlDataAdapter(cmdStr, conn);
               
                sdp.Fill(ds);
                conn.Close();
                return ds;
            }
        }

        /// <summary>
        /// 加载模板文件
        /// </summary>
        public void CreateCodeFile(string connStr,string cmdStr)
        {
            _sbMain = new StringBuilder();
            using (StreamReader sdr = new StreamReader("Template\\paramTemplate.tt",Encoding.UTF8))
            {
                string strTmp = "";
                while (strTmp != null)
                {
                    strTmp = sdr.ReadLine();
                    if (!string.IsNullOrEmpty(strTmp))
                        _sbMain.AppendLine(strTmp);
                }
                sdr.Close();
            }
            var ds = GetTableInfo(connStr, cmdStr);
            if (ds != null)
            {
                var dt = ds.Tables[0];
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (i > 0)
                        sb.AppendLine(" public " + dt.Columns[i].DataType.Name + "  " + dt.Columns[i].ColumnName + " { get; set; }");
                    else 
                        sb.AppendLine(" public " + dt.Columns[i].DataType.Name + "  " + dt.Columns[i].ColumnName + " { get; set; }");
                }
                _sbMain.Replace(CODECREATE_PUBLICEPROPERTY, sb.ToString());
                _sbMain.Replace("$CreateTime$", DateTime.Now.ToString());
                string path = @"D:\Param\param.cs";
                using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
                {
                    sw.Write(_sbMain.ToString());
                    sw.Close();
                }
            }
        }

        public string CreateGridCode(string connStr, string cmdStr)
        {
            var ds = GetTableInfo(connStr, cmdStr);
            if (ds != null && ds.Tables.Count>0)
            {
                var dt = ds.Tables[0];
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    string name = dt.Columns[i].ColumnName;
                    sb.AppendLine(string.Format("<asp:BoundField DataField=\"{0}\" HeaderText=\"{0}\" />",name));
                    
                }
                return sb.ToString();
            }
            return "";
        }
    }
}
